package com.wjy.sfhcore.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;

import com.wjy.sfhcore.R;
import com.wjy.sfhcore.itf.IntentListener;
import com.wjy.sfhcore.itf.impl.ActivityIntentFactory;
import com.wjy.sfhcore.ui.widget.MyProgressBar;

/**
 * Created by lawrence on 15/7/31.
 * activity基类
 */
public abstract class CoreActivity extends AppCompatActivity {
    public final String TAG = getClass().getSimpleName();
    /**
     * 参数传递标示
     */
    public static final String PARAM_INTENT = "intentData";
    /**
     * 传递参数
     */
    private Bundle intentData;


    /**
     * 模态对话框
     */
    public MyProgressBar mProgressBar;


    private IntentListener intentFactory;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mProgressBar = new MyProgressBar(this);
        intentFactory = new ActivityIntentFactory(this);
        if (savedInstanceState == null) {
            intentData = getIntent().getExtras();
        } else {
            intentData = savedInstanceState.getBundle(PARAM_INTENT);
        }
        Bundle bundle = intentData != null
                && intentData.getBundle(PARAM_INTENT) != null ? intentData
                .getBundle(PARAM_INTENT) : intentData;
        initIntentData(bundle != null ? bundle : new Bundle());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lib_core_activity);
        initContainer();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    /**
     * 初始化页面传递过来的数据
     *
     * @param bundle 数据
     */
    public void initIntentData(Bundle bundle) {

    }


    /**
     * 初始化显示容器
     */
    private void initContainer() {
        addContainer();
    }

    /**
     * 将作为内容显示的fragment加入容器
     */
    public abstract void addContainer();


    /****************************************/
    /**
     * 往容器里面添加fragment
     *
     * @param fragment
     */
    public void addFragment(Fragment fragment) {
        intentFactory.addFragment(fragment);
    }

    /**
     * 替换容器里面的fragment
     *
     * @param newFragment
     */
    public void replaceFragment(Fragment newFragment) {
        intentFactory.replaceFragment(newFragment);
    }

    /**
     * fragment替换
     *
     * @param newFragment    碎片
     * @param addToBackStack 是否保存堆栈信息
     */
    public void replaceFragment(Fragment newFragment, boolean addToBackStack) {
        intentFactory.replaceFragment(newFragment, addToBackStack);
    }

    /**
     * fragment替换
     *
     * @param contentID      容器ID
     * @param newFragment    碎片
     * @param addToBackStack 是否保存堆栈信息
     */
    public void replaceFragment(int contentID, Fragment newFragment,
                                boolean addToBackStack) {
        intentFactory.replaceFragment(contentID, newFragment, addToBackStack);

    }

    /**
     * 单纯的页面跳转
     *
     * @param cls 跳转的页面
     */
    public void goToOthers(Class<?> cls) {
        intentFactory.goToOthers(cls);
    }

    /**
     * 页面跳转并关闭当前页面
     *
     * @param cls 跳转的页面
     */
    public void goToOthersF(Class<?> cls) {
        intentFactory.goToOthersF(cls);
    }

    /**
     * 带参数的页面跳转
     *
     * @param cls    跳转的页面
     * @param bundle 参数
     */
    public void goToOthers(Class<?> cls, Bundle bundle) {
        intentFactory.goToOthers(cls, bundle);
    }

    /**
     * 带参数的页面跳转并关闭当前页面
     *
     * @param cls    跳转的页面
     * @param bundle 参数
     */
    public void goToOthersF(Class<?> cls, Bundle bundle) {
        intentFactory.goToOthersF(cls, bundle);
    }

    /**
     * 带回调的页面跳转
     *
     * @param cls         跳转的页面
     * @param bundle      参数
     * @param requestCode 请求码
     */
    public void goToOthersForResult(Class<?> cls, Bundle bundle, int requestCode) {
        intentFactory.goToOthersForResult(cls, bundle, requestCode);
    }

    /**
     * 设置回调
     *
     * @param cls        回调的页面
     * @param bundle     参数
     * @param resultCode 返回码
     */
    public void backForResult(Class<?> cls, Bundle bundle, int resultCode) {
        intentFactory.backForResult(cls, bundle, resultCode);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Bundle bundle = null;
        if (data != null) {
            bundle = data.getBundleExtra(PARAM_INTENT);
            if (bundle == null) {
                bundle = data.getExtras();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
        onActivityResult(requestCode, resultCode, bundle);
    }

    /**
     * 页面回调函数
     *
     * @param requestCode 请求码
     * @param resultCode  返回码
     * @param data        数据
     */
    protected void onActivityResult(int requestCode, int resultCode, Bundle data) {

    }

}
